package org.example;

import java.io.*;
import java.nio.charset.Charset;

public class IO_Demo16 {
    public static void main(String[] args) throws IOException {
        //转换流 InputStreamReader是转换流 可以将字节流转换为字符流 解决出现乱码问题
        //InputStreamReader 就像字节流的“翻译官”，将原始字节数据按照指定字符集翻译成 Java 能理解的 Unicode 字符。按指定方式解码
        InputStreamReader isr=new InputStreamReader(new FileInputStream("文件路径"),"gbk");
        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("文件路径"),"UTF-8");
        int ch;
        while((ch=isr.read())!=-1){
          osw.write(ch);
        }
        isr.close();
        //第二种方法(jdk11之后)
        FileReader fr=new FileReader("文件路径", Charset.forName("gbk"));
        FileWriter fw=new FileWriter("文件路径",Charset.forName("UTF-8"));
        int ch1;
        while((ch1=fr.read())!=-1){
            fw.write(ch1);
        }
        fr.close();

    }
    //字节流：FileInputStream (读取字节) →
    //转换流：InputStreamReader (字节→字符，编码处理) →
    //字符流：BufferedReader (高效字符读取)



}
